
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.*;

import org.hibernate.Session;
import org.hibernate.*;
/**
 * Servlet implementation class rate
 */
@WebServlet("/rate")
public class rate extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public rate() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession guestSession = request.getSession(true);
		int grade = Integer.parseInt(request.getParameter("rating"));
		int guestId = Integer.parseInt(guestSession.getAttribute("guestUserID").toString());
		//sSystem.out.println(guestId);
		//int guestId = 2;
		int hostFamilyId = 1;
		List result2 = null;
        PrintWriter out = response.getWriter();
		rating ratingObj = new rating();
		
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	        session.beginTransaction();
	        String sqlString2 = "select * from rating where GuestUserID = " + guestId + " and hostFamilyID = " + hostFamilyId + "";
	        Query queryResult2 = session.createSQLQuery(sqlString2).addEntity(rating.class);
	        result2 = queryResult2.list();
	        if(result2==null || result2.isEmpty()){
	        	ratingObj.setGrade(grade);
	        	ratingObj.setGuestUserId(guestId);
	        	ratingObj.setHostFamilyId(1);
	        	session.saveOrUpdate(ratingObj);
	        	session.getTransaction().commit();
	        	//HibernateUtil.getSessionFactory().close();
	        	request.setAttribute("ratingObj", ratingObj);
	        	request.getRequestDispatcher("rating.html").forward(request, response);
	        }
	        else{
	        	ratingObj = (rating) result2.get(0);
	        	ratingObj.setGrade(grade);
		        ratingObj.setGuestUserId(guestId);
		    	ratingObj.setHostFamilyId(1);
		        session.saveOrUpdate(ratingObj);
		        session.getTransaction().commit();
		        //HibernateUtil.getSessionFactory().close();
		        request.setAttribute("ratingObj", ratingObj);
		        request.getRequestDispatcher("rating.html").forward(request, response);
	        }
			} catch(Exception ex){
				HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
			}
	}
		
}
